package org.examples.javax.script;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.util.List;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.SimpleBindings;

import org.junit.Test;

public class ScriptEngineFactoryTest
{
    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();

    @Test
    public void testSetBindings()
    {
        Bindings bindings = new SimpleBindings();
        scriptEngineManager.setBindings(bindings);
        assertEquals(bindings, scriptEngineManager.getBindings());
    }

    @Test
    public void testGetEngineFactories()
    {
        List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories();
        assertEquals(1, engineFactories.size());
        ScriptEngineFactory rhinoEngineFactory = engineFactories.get(0);
        assertEquals("Mozilla Rhino", rhinoEngineFactory.getEngineName());
    }



    @Test
    public void testScriptEngineManagerExtClassLoader()
    {
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager(null);
        List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories();
        assertEquals(1, engineFactories.size());
        ScriptEngineFactory rhinoEngineFactory = engineFactories.get(0);
        assertEquals("Mozilla Rhino", rhinoEngineFactory.getEngineName());
    }

    @Test
    public void testGetEngineByName()
    {
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("JavaScript");
        assertNotNull(scriptEngine);
    }

    @Test
    public void testGetEngineByExtension()
    {
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByExtension("js");
        assertNotNull(scriptEngine);
    }

    @Test
    public void testGetEngineByMimeType()
    {
        ScriptEngine scriptEngine = scriptEngineManager.getEngineByMimeType("text/javascript");
        assertNotNull(scriptEngine);
    }

    @Test
    public void testGet()
    {
        scriptEngineManager.put("Abc", 1);
        Object object = scriptEngineManager.get("Abc");
        assertEquals(1, object);
    }

    @Test
    public void testRegisterEngineName()
    {
        List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories();
        ScriptEngineFactory rhinoEngineFactory = engineFactories.get(0);

        scriptEngineManager.registerEngineName("New name", rhinoEngineFactory);
        assertNotNull(scriptEngineManager.getEngineByName("New name"));
    }

    @Test
    public void testRegisterEngineMimeType()
    {
        List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories();
        ScriptEngineFactory rhinoEngineFactory = engineFactories.get(0);

        scriptEngineManager.registerEngineMimeType("New mime", rhinoEngineFactory);
        assertNotNull(scriptEngineManager.getEngineByMimeType("New mime"));
    }

    @Test
    public void testRegisterEngineExtension()
    {
        List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories();
        ScriptEngineFactory rhinoEngineFactory = engineFactories.get(0);

        scriptEngineManager.registerEngineExtension("js2", rhinoEngineFactory);
        assertNotNull(scriptEngineManager.getEngineByExtension("js2"));
    }

}
